package be.kuleuven.mume.beans;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import be.kuleuven.mume.PMF;
import be.kuleuven.mume.query.DeleteQueryHelper;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class AgendaBean {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key id;
	@Persistent
	private String omschrijving;
	@Persistent
	private Date datum;
	@Persistent
	private int hours; //duur
	@Persistent
	private int minutes;//duur
	@Persistent
	private String locatie;
	@Persistent
	private String type;
	@Persistent
	private String met;
	@Persistent
	private Key userId;
	@Persistent
	private Key vakId;
	@Persistent
	private Date timeStamp;

	public AgendaBean(Calendar calendar, String type) {
		this.datum = calendar.getTime();
		this.timeStamp = Calendar.getInstance().getTime();
		this.type = type;
	}

	public void setAgendaId(String id) {
		this.id = KeyFactory.stringToKey(id);
	}
	
	public String getId() {
		return KeyFactory.keyToString(id);
	}

	public String getOmschrijving() {
		return omschrijving;
	}

	public void setOmschrijving(String omschrijving) {
		this.omschrijving = omschrijving;
	}
	
	public String getLocatie() {
		return locatie;
	}

	public void setLocatie(String locatie) {
		this.locatie = locatie;
	}

	public Date getDatum() {
		return datum;
	}

	public void setDatum(Date datum) {
		this.datum = datum;
	}
	
	public int getHours() {
		return hours;
	}

	public void setHours(int hours) {
		this.hours = hours;
	}

	public int getMinutes() {
		return minutes;
	}

	public void setMinutes(int minutes) {
		this.minutes = minutes;
	}

	public String getType() {
		return type;
	}
	
	public Date getEinde() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(datum);
		calendar.add(Calendar.HOUR_OF_DAY, getHours());
		calendar.add(Calendar.MINUTE, getMinutes());
		return calendar.getTime();
	}

	public String getMet() {
		return met;
	}

	public void setMet(String met) {
		this.met = met;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getUserId() {
		return KeyFactory.keyToString(userId);
	}

	public void setUserId(Key userId) {
		this.userId = userId;
	}
	
	public String getVakId() {
		PersistenceManager pm = PMF.getPersistenceManager();
		VakBean vak = pm.getObjectById(VakBean.class, this.vakId);
		pm.close();
		return vak.getVakId();
	}

	public String getVakName() {
		PersistenceManager pm = PMF.getPersistenceManager();
		VakBean vak = pm.getObjectById(VakBean.class, this.vakId);
		pm.close();
		return vak.getName();
	}

	public void setVakId(Key vakId) {
		this.vakId = vakId;
	}
	
	public Date getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(Date timeStamp) {
		this.timeStamp = timeStamp;
	}

	public void store()
	{
		PersistenceManager pm = PMF.getPersistenceManager();
		pm.makePersistent(this);
		pm.close();
	}
	
	public void delete(){
		DeleteQueryHelper h = new DeleteQueryHelper(this.getId(), this.getClass());
		h.executeAtomicQuery();
	}
	
	public static class AgendaBeanFactory{
		@SuppressWarnings("unchecked")
		public static List<AgendaBean> getAfspraken(PersoonBean persoon, long page, long rpp) {
			PersistenceManager pm = PMF.getPersistenceManager();
			//q.setRange(page*rpp,(page+1)*rpp);
			String persId = persoon.getId();
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_MONTH,-1);
			Date yesterday = calendar.getTime();
			
			Query q = pm.newQuery(AgendaBean.class);
			q.setFilter("this.datum >= yesterday && (this.userId == null || this.userId == persId)");
			q.declareImports("import java.util.Date;import com.google.appengine.api.datastore.Key");
			q.declareParameters("Date yesterday, Key persId");
			q.setOrdering("this.datum ascending");
			return (List<AgendaBean>)q.execute(yesterday, KeyFactory.stringToKey(persId));
		}
		
		@SuppressWarnings("unchecked")
		public static List<AgendaBean> getUpdates(PersoonBean persoon, long page, long rpp) {
			PersistenceManager pm = PMF.getPersistenceManager();
			//q.setRange(page*rpp,(page+1)*rpp);
			String persId = persoon.getId();
			
			Query q = pm.newQuery(AgendaBean.class);
			q.setFilter("(this.userId == null || this.userId == persId) && (this.type == 'deadline' || this.type == 'afspraak')");
			q.declareImports("import com.google.appengine.api.datastore.Key");
			q.declareParameters("Key persId");
			q.setOrdering("this.timeStamp descending");
			return (List<AgendaBean>)q.execute(KeyFactory.stringToKey(persId));
		}
	}
}
